package com.woniu.cq.mallproduct.controller;


import com.woniu.cq.mallproduct.common.Result;
import com.woniu.cq.mallproduct.domain.Product;
import com.woniu.cq.mallproduct.params.CourtParams;
import com.woniu.cq.mallproduct.params.ProductParams;
import com.woniu.cq.mallproduct.service.IProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.sql.ResultSet;
import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author daniel
 * @since 2020-09-19
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService productService;

    @GetMapping("/getProducts")
    public Result getProducts(){
        List<Product> productList =productService.getProducts();
        return Result.builder().code(200).data(productList).msg("查询成功").build();
    }
    @PostMapping("/addProduct")
    public Result addProduct(ProductParams productParams) {
        Product product = new Product();
        BeanUtils.copyProperties(productParams, product);
        productService.addProduct(product);
        return Result.builder().code(200).msg("新增成功").data(product).build();
    }

    @GetMapping("/upProduct")
    public Result upProduct(Integer productId) {
        try {
            int i = productService.upProduct(productId);
            if (i == 0) {
                return Result.builder().msg("上架成功").code(200).data(productId).build();
            } else {
                return Result.builder().data(productId).code(500).msg("上架失败").build();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.builder().data(productId).code(500).msg("上架失败").build();
        }
    }

    @GetMapping("/downProduct")
    public Result downProduct(Integer productId) {
        try {
            productService.downProduct(productId);
            return Result.builder().msg("下架成功").code(200).data(productId).build();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.builder().msg("下架失败,商品不存在").code(500).build();
        }
    }

    @PostMapping("/deleteProduct")
    public Result deleteProduct(Integer productId) {
        try {
            productService.deleteProduct(productId);
            return Result.builder().code(200).msg("删除成功").data(productId).build();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.builder().msg("删除失败").code(500).build();
        }

    }

    @GetMapping("/getProduct")
    public Result getProduct() {
        try {
            List<Product> product = productService.getProduct();
            return Result.builder().msg("查询成功").code(200).data(product).build();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.builder().code(500).msg("获取失败").build();
        }
    }

    @GetMapping("/getOne")
    public Result getOneProduct(Integer productId) {
        try {
            Product oneProduct = productService.getOneProduct(productId);
            return Result.builder().code(200).msg("查询成功").data(oneProduct).build();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.builder().code(500).msg("获取失败").build();
        }
    }

    @GetMapping("/getLikeProduct")
    public Result getLikeProduct(String name, String info) {
        List<Product> likeProduct = productService.getLikeProduct(name, info);
        return Result.builder().data(likeProduct).msg("查询成功").code(200).build();
    }

    @GetMapping("/buy")
    public Result buy(Integer userId) {
        try {
            int orderId = productService.buy(userId);
            if (orderId > 0) {
                return Result.builder().code(200).data(orderId).msg("新增成功").build();
            }
            return Result.builder().code(500).msg("新增失败").build();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.builder().code(500).msg("获取失败").build();
        }
    }

    @PostMapping("/addCourt")
    public Result addCourt(CourtParams courtParams) {
        Integer number = courtParams.getNumber();
        Integer productId = courtParams.getProductId();
        Integer userId = courtParams.getUserId();
        try {
            productService.addCourt(productId, number, userId);
            return Result.builder().msg("加入购物车成功").code(200).build();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.builder().code(500).msg("加入购物车失败").build();
        }
    }

    @PostMapping("/buyNow")
    public Result buyNow(CourtParams courtParams) {
        Integer userId = courtParams.getUserId();
        try {
            addCourt(courtParams);
            buy(userId);
            return Result.builder().code(200).msg("购买成功").build();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.builder().code(500).msg("加入购物车失败").build();
        }
    }
}

